﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.logging;
using com.eforceglobal.crux.logging.Enums;
using com.eforceglobal.crux.exceptions;

namespace com.eforceglobal.crux.controllers.Administration
{
    public class ManageCompaniesController
    {
        IManageCompanies _view = null;
        public ManageCompaniesController(IManageCompanies view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if (!IsPostBack)
                SwitchToListMode();
        }

        public void OpenCompany(int CompanyID)
        {
            CompanyFacade companyFacade = new CompanyFacade();
            CompanyTypeFacade companyTypeFacade = new CompanyTypeFacade();
            CompanyInformation companyInformation = new CompanyInformation();
            companyInformation = companyFacade.GetCompany(CompanyID);
            _view.AllowedCompanyTypes = companyTypeFacade.GetAllCompanyTypes();
            _view.CurrentCompany = companyInformation;
            _view.FormMode = FormMode.AddEdit;
        }

        public void CreateCompany()
        {
            OpenCompany(0);
        }

        public void SaveCompany()
        {
            try
            {
                CompanyFacade companyFacade = new CompanyFacade();
                companyFacade.SaveCompany(_view.CurrentCompany);
                SwitchToListMode();
            }
            catch (CruxDatabaseException ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError(ex.ErrorNameOrCode, ex.Message);
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }

        }

        private void SwitchToListMode()
        {
            _view.ErrorDetails = null;
            _view.CurrentCompany = new CompanyInformation();
            CompanyFacade facade = new CompanyFacade();
            _view.Companies = facade.GetCompanies();
            _view.FormMode = FormMode.List;
        }

        public void CancelAddEdit()
        {
            SwitchToListMode();
        }

        public void DeleteCompany(int CompanyID)
        {

            try
            {
                CompanyFacade facade = new CompanyFacade();
                facade.DeleteCompany(CompanyID);
                SwitchToListMode();
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }
        }

        public void OpenCompanyAdministrationView(int CompanyID)
        {
            NavigationController.OpenView("ManageCompany", CompanyID);
        }
    }
}
